home *** CD-ROM | disk | FTP | other *** search
- // ============================================================
- // LeaRNWaRe code by CROM / Spanish Lords
- // - since 1993 -
- //
- // Objetivo : Manejo de un buffer canalizado - Polling.
- // Autor : Pedro Ant≤n Alonso. crom@ergos.es
- // Plataforma : Windows 95 / NT & Direct X
- // Compilador : Visual C++ 6.0
- //
- // ============================================================
- #include "DSUtiles.h"
-
- //
- // INIT
- //
- BOOL CDSUtil::Init( HWND hwnd, HINSTANCE hinst, GUID *pguid )
- {
- HRESULT hr;
- DSBUFFERDESC DSBDesc;
- LPDIRECTSOUNDBUFFER lpDSBPrimary;
- WAVEFORMATEX wavFormat;
-
-
- bPlaying=FALSE;
- CoInitialize (NULL);
- if (FAILED(hr=DirectSoundCreate(pguid,&lpDS,NULL))) return FALSE;
- if (FAILED(hr=lpDS->SetCooperativeLevel(hwnd,DSSCL_PRIORITY))) return FALSE;
- ZeroMemory( &DSBDesc,sizeof(DSBUFFERDESC) );
- DSBDesc.dwSize = sizeof(DSBUFFERDESC);
- DSBDesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
- DSBDesc.dwBufferBytes = 0;
- DSBDesc.lpwfxFormat = NULL;
- if( FAILED(hr=lpDS->CreateSoundBuffer(&DSBDesc,&lpDSBPrimary,NULL))) return FALSE;
- ZeroMemory(&wavFormat, sizeof(WAVEFORMATEX) );
- wavFormat.wFormatTag = WAVE_FORMAT_PCM;
- wavFormat.nChannels = 2;
- wavFormat.nSamplesPerSec = 22050;
- wavFormat.wBitsPerSample = 16;
- wavFormat.nBlockAlign = wavFormat.wBitsPerSample / 8 * wavFormat.nChannels;
- wavFormat.nAvgBytesPerSec = wavFormat.nSamplesPerSec * wavFormat.nBlockAlign;
- if(FAILED(hr=lpDSBPrimary->SetFormat(&wavFormat))) return FALSE;
- lpDSBPrimary->Release();
- return TRUE;
- }
-
- //
- // END
- //
- void CDSUtil::End ()
- {
- StopPlay();
- WaveCloseReadFile(&hmmio, &pWaveFormat);
- CoUninitialize();
- if (lpDSB) lpDSB->Release();
- if (lpDS) lpDS->Release();
- }
-
- //
- // PLAY
- //
- BOOL CDSUtil::Play ()
- {
- HRESULT hr;
- DWORD dwPlay;
- DWORD dwStartOffset;
- VOID *lpvData;
- DWORD dwBytesLocked;
- UINT uiBytesRead;
-
- if (lpDSB==NULL) return FALSE;
-
- if (FAILED(lpDSB->GetCurrentPosition(&dwPlay,NULL))) return FALSE;
-
- if (((dwPlay>=dwMiddleBuffer )&&(dwLastPlayPos<dwMiddleBuffer))||(dwPlay<dwLastPlayPos))
- {
- if (dwPlay>=dwMiddleBuffer) dwStartOffset = 0;
- else dwStartOffset = dwMiddleBuffer;
-
- hr = lpDSB->Lock(dwStartOffset, dwMiddleBuffer,&lpvData,&dwBytesLocked,NULL,NULL,0);
- WaveReadFile ( hmmio, dwBytesLocked,(BYTE *)lpvData,&mmckInfo,&uiBytesRead );
- if (uiBytesRead<dwBytesLocked)
- {
- if (WaveStartDataRead(&hmmio,&mmckInfo,&mmckInfoParent)!=0) return FALSE;
- else
- WaveReadFile(hmmio,dwBytesLocked-uiBytesRead,(BYTE *)lpvData + uiBytesRead,
- &mmckInfo,&uiBytesRead );
- }
- lpDSB->Unlock(lpvData, dwBytesLocked, NULL, 0 );
- }
- dwLastPlayPos = dwPlay;
- return TRUE;
- }
-
- //
- // LOADWAVINBUFFER
- //
- BOOL CDSUtil::LoadWavInBuffer (LPSTR FileName)
- {
- DSBUFFERDESC DSBDesc;
- HRESULT hr;
-
- WaveCloseReadFile( &hmmio, &pWaveFormat );
- if (lpDSB!=NULL)
- {
- lpDSB->Release();
- lpDSB = NULL;
- }
-
- if (WaveOpenFile(FileName,&hmmio,&pWaveFormat,&mmckInfoParent )!=0) return FALSE;
- if (WaveStartDataRead(&hmmio,&mmckInfo,&mmckInfoParent)!=0) return FALSE;
-
- memset( &DSBDesc, 0, sizeof( DSBUFFERDESC ) );
- DSBDesc.dwSize = sizeof( DSBUFFERDESC );
- DSBDesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_GLOBALFOCUS;
- DSBDesc.dwBufferBytes = pWaveFormat->nAvgBytesPerSec*2;
- DSBDesc.lpwfxFormat = pWaveFormat;
-
- if (FAILED(hr=lpDS->CreateSoundBuffer(&DSBDesc,&lpDSB,NULL)))
- {
- WaveCloseReadFile(&hmmio,&pWaveFormat);
- return FALSE;
- }
-
- ClearBuffer();
- dwMiddleBuffer = DSBDesc.dwBufferBytes/2;
- return TRUE;
- }
-
- //
- // STARTPLAY
- //
- void CDSUtil::StartPlay()
- {
- HRESULT hr;
-
- if (bPlaying==FALSE)
- {
- hr = lpDSB->Play(0,0,DSBPLAY_LOOPING);
- bPlaying=TRUE;
- }
- }
-
- //
- // STOPPLAY
- //
- void CDSUtil::StopPlay()
- {
- HRESULT hr;
-
- if (bPlaying==TRUE)
- {
- hr = lpDSB->Stop();
- hr = lpDSB->SetCurrentPosition( 0L );
- bPlaying=FALSE;
- }
- }
-
- //
- // CLEARBUFFER
- //
- BOOL CDSUtil::ClearBuffer ()
- {
- WAVEFORMATEX WaveFormat;
- DWORD dwSizeWritten;
-
- PBYTE pPtr;
- DWORD dwSize;
-
- if (FAILED( lpDSB->GetFormat(&WaveFormat,sizeof(WAVEFORMATEX),&dwSizeWritten))) return FALSE;
- if ( SUCCEEDED(lpDSB->Lock(0,0,(LPVOID *)&pPtr,&dwSize,NULL,NULL,DSBLOCK_ENTIREBUFFER)))
- {
- if (WaveFormat.wBitsPerSample==8) FillMemory(pPtr,dwSize,128);
- else FillMemory(pPtr,dwSize,0);
- lpDSB->Unlock(pPtr,dwSize,NULL,0);
- return TRUE;
- }
- else return FALSE;
- }
-